<?php

namespace MoChat\ClientNew\Action;

use Carbon\Carbon;
use Hyperf\DbConnection\Db;
use MoChat\ClientNew\Model\ClientFollowRecord;
use MoChat\ClientNew\Model\ClientNew;
use MoChat\Framework\Action\AbstractAction;
use MoChat\Framework\Constants\ErrorCode;
use MoChat\Framework\Exception\CommonException;
use MoChat\Framework\Request\ValidateSceneTrait;
use MoChat\App\Common\Middleware\DashboardAuthMiddleware;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Qbhy\HyperfAuth\AuthManager;

/**
 * @Controller
 */
class FollowAdd extends AbstractAction
{
    use ValidateSceneTrait;


    protected $auth;

    /**
     * @Middlewares({
     *     @Middleware(DashboardAuthMiddleware::class)
     * })
     * @RequestMapping(path="/client-new/followAdd", methods="post")
     */
    public function handle(): array
    {
        $params = $this->request->all();
        $user =user();
        if (empty($user)) {
            throw new CommonException(ErrorCode::AUTH_UNAUTHORIZED, '登入异常请重新登入');
        }
        $follow=[
            'type'=>$params['type'],
            'client_id'=>$params['clientId'],
            'user_id'=>Db::table('work_employee')->where('log_user_id',$user['id'])->value('id'),
            'remark'=>$params['remark'],
            'created_at'=>date('Y-m-d H:i:s'),
            'followed_at'=>$params['followedAt'],
            'returned_at'=>$params['returnedAt']?$params['returnedAt']:null,
            'work_contact_id'=>$params['work_contact_id']??0
        ];
        ClientFollowRecord::query()->insert($follow);
        return [];
    }
}
